let Global = require("Global");
let BKTools = require("BKTools");
let NetUtils = require("NetUtils");
let customComponent = require("CustomComponent");

cc.Class({
    extends: customComponent,

    properties: {

    },

    toBack() {
        cc.director.loadScene("Scene");
    },

    // onLoad () {},

    start() {
        this._super();
    },

    toLogin() {
        let self = this;
        if (cc.sys.platform == cc.sys.QQ_PLAY) {
            BKTools.log("QQ玩一玩....");
            Global.isWeb = false;
            // Global.gameId = GameStatusInfo.gameId;
            Global.openId = GameStatusInfo.openId;
            Global.platform = GameStatusInfo.platform == "android" ? 2 : 1;
            Global.sex = GameStatusInfo.sex;
            Global.osVersion = GameStatusInfo.osVersion;

            if (GameStatusInfo.gameParam) {
                Global.inviter = GameStatusInfo.gameParam;
            }
            BKTools.getNick(function (openId, nick) {
                Global.nickName = nick;
                NetUtils.doLogin(function (code) {
                    if (code == 0) {
                        BKTools.log("QQPlay 登录成功...");
                        self.initWebSocket();
                        BKTools.log("QQPlay initWebSocket...");
                    }
                });
            });
        } else {
            BKTools.log("非QQ玩一玩....");
            NetUtils.doLogin(function (code) {
                if (code == 0) {
                    BKTools.log("web 登录成功...");
                    this.initWebSocket();
                }
            }.bind(this));
        }
    },

    initWebSocket() {
        if (Global.WEB_SOCKET.URL) {
            BKTools.log("初始化webSocket..." + Global.WEB_SOCKET.URL);
            cc.log(this._webSocket);
            this._webSocket.initWebSocket();
        } else {
            BKTools.log("webSocket 参数异常");
        }
    },

    // update (dt) {},
});